<?php

/**
 * Created
 * User: zuoa
 * Date: 2016/11/21 0021
 * Time: 19:35
 */
class Violation extends Y_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('file');
    }

    /**
     * 违法教育
     */
    public function index()
    {
        $this->check_login();

        date_default_timezone_set("Asia/Shanghai");
        $this_month_begin = date("Y-m-01 00:00:00");
        $this_month_end = date("Y-m-d H:i:s");
        $this_month_all_count = $this->violation_model->get_total_count($this_month_begin, $this_month_end, $this->get_dept());


        $last_month_begin = date("Y-m-01 00:00:00", strtotime("$this_month_begin -1 month"));
        $last_month_end = date("Y-m-d H:i:s", strtotime("$this_month_end -1 month"));
        $last_month_all_count = $this->violation_model->get_total_count($last_month_begin, $last_month_end, $this->get_dept());

        $violation_ranks = $this->violation_model->get_violation_ranks(5, $this->get_dept());
        for ($i = 0; $i < count($violation_ranks); $i++) {
            $violation_ranks[$i]["rank"] = $i + 1;
            if ($this_month_all_count > 0) {
                $violation_ranks[$i]["percent"] = $violation_ranks[$i]["count"] * 100 / $this_month_all_count;
            } else {
                $violation_ranks[$i]["percent"] = 0;
            }
        }

        $data["m"] = date("Y-m");

        $data["this_month_all_count"] = $this_month_all_count;
        $data["last_month_all_count"] = $last_month_all_count;
        $data["uprate"] = $last_month_all_count ? ($this_month_all_count - $last_month_all_count) * 100 / $last_month_all_count : 0;
        $data["violation_ranks"] = $violation_ranks;
        $data["dept_ranks"] = $this->violation_model->get_department_ranks($this_month_begin, $this_month_end);
        $data["police_ranks"] = $this->violation_model->get_police_ranks($this_month_begin, $this_month_end, 10, $this->get_dept());
        $data["violator_ranks"] = $this->violation_model->get_violator_ranks($this_month_begin, $this_month_end);


        $this->load->view('dashboard/header', $this->_get_context());
        $this->load->view('dashboard/wzjy/statistics', $data);
        $this->load->view('dashboard/footer');
    }

    public function json()
    {
        $this->check_login();

        $type = $this->input->get("t");

        switch ($type) {
            case 'wzjy_by_day_in_month': {
                $num = $this->input->get("n");
                if (!is_int($num) || $num == 0) {
                    $num = 10;
                }

                if ($this->is_admin()) {
                    $result = $this->violation_model->get_statistics_by_day();
                    $this->response(200, "success", $result);
                } else {
                    $user_data = $this->session->userdata();
                    $result = $this->violation_model->get_statistics_by_day($user_data['userdept']);
                    $this->response(200, "success", $result);
                }

                break;
            }
            default: {
                echo '{}';
                break;
            }
        }
    }

    function export($month)
    {
        $this->check_login();
        $csv = $this->violation_model->get_export_result();
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/csv', 'utf-8')
            ->set_output($csv)
            ->_display();
        exit;
    }

}